#include "ctclientsocket.h"
//#include <QNetworkProxy>
#include "global.h"

CTClientSocket::CTClientSocket(QObject *parent)
    : QObject{parent}
{
    m_tcpSocket = new QTcpSocket(this);
    m_recvBuffer.clear();
    //moveToThread(QThreadPool::globalInstance()->thread());
    //qDebug() << "CTClientSocket moved to thread:" << thread();
    connect(m_tcpSocket, &QTcpSocket::connected, this, &CTClientSocket::sendMessage);
    connect(m_tcpSocket, &QTcpSocket::readyRead, this, &CTClientSocket::handleDataReceived);
    connect(m_tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::errorOccurred), this, &CTClientSocket::socketError);

}
CTClientSocket::~CTClientSocket()
{
    qDebug()<<"~CTClientSocket()";
    delete m_tcpSocket;
}
void CTClientSocket::setServerAddress(const QString &address)
{
    m_serverAddress = address;
}

void CTClientSocket::setServerPort(int port)
{
    m_serverPort = port;
}

void CTClientSocket::setSendMsg(QString sendMsg)
{
    m_sendMsg = sendMsg;
}

void CTClientSocket::setMsgFlag(QString msgFlag,QString requestId)
{
    m_flag = msgFlag;
    m_requestId= requestId;
}

void CTClientSocket::setRequestParam(QString reqParam)
{
    m_reqParam = reqParam;
}

void CTClientSocket::startConnection()
{
    QString proxyStr = g_proxyPool.getNextProxy();
    if(proxyStr.length()==0)
    {
        qDebug()<<"获取代理失败";
        return;
    }
    QStringList stringList = proxyStr.split(":");
    QNetworkProxy socks5Proxy(
        QNetworkProxy::Socks5Proxy,
        stringList.at(0),
        stringList.at(1).toInt()
        );

    m_tcpSocket->setProxy(socks5Proxy);
    qDebug()<<"connet server"<<m_reqParam<<socks5Proxy.hostName()<<m_serverAddress<<m_serverPort;
    m_tcpSocket->connectToHost(m_serverAddress, m_serverPort);
    //qDebug()<<999<<m_tcpSocket->state();
}

QString CTClientSocket::getRequestParam()
{
    return m_reqParam;
}

void CTClientSocket::run()
{
    startConnection();

}

void CTClientSocket::sendMessage()
{
    if (m_tcpSocket->state() != QAbstractSocket::ConnectedState)
        return;

    m_tcpSocket->write(m_sendMsg.toUtf8()+ "\n");
    m_tcpSocket->flush();
    qDebug()<<"send msg"<<m_reqParam<<m_serverAddress<<m_serverPort<<m_sendMsg;
}
void CTClientSocket::handleDataReceived()
{
    while (m_tcpSocket->bytesAvailable())
    {
        m_recvBuffer += m_tcpSocket->readAll();
    }
    //qDebug()<<"recv msg"<<m_reqParam<<m_recvBuffer.length();
    //emit dataReceived(data, m_requestId);  // 替换为实际的请求ID
}
void CTClientSocket::socketError(QAbstractSocket::SocketError error)
{

    switch (error) {
    case QAbstractSocket::RemoteHostClosedError:
        qDebug() << "Server has closed connection"<<m_reqParam<<m_recvBuffer.length();//<<m_recvBuffer;
        if(m_recvBuffer.length())//有数据才发送信息进行处理
        {
            emit dataSocketReceived(m_recvBuffer, m_reqParam);  // 替换为实际的请求ID
            //qDebug() <<"emit dataSocketReceived "<<m_reqParam;
        }
        break;
    default:
        qWarning() << "An error occurred: " << m_tcpSocket->errorString();
        emit dataSocketReceived(m_recvBuffer, m_reqParam);
//        if(m_recvBuffer.length())//有数据才发送信息进行处理
//        {
//            emit dataSocketReceived(m_recvBuffer, m_reqParam);  // 替换为实际的请求ID
//        }
        break;
    }
    //deleteLater();
}
